package org.gc.baseframe.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * <p>Title: </p>
 * <p>Description: Type Converter</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: </p>
 * <p>LastModified: </p>
 * @author wangdong
 * @version 1.0
 */
public class TypeConvert {

    /**
     * ResultSet to List
     * @param rs ResultSet
     * @throws SQLException
     * @return List
     */
    public static List ResultSetToList(ResultSet rs)
        throws SQLException {
        List data = new ArrayList();
        while(rs.next()) {
            List row = new ArrayList();
            for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++){
                row.add(rs.getString(i));
            }
            data.add(row);
        }
        rs.close();
        return data;
    }

    /**
     * ResultSet to Vector
     * @param rs ResultSet
     * @throws SQLException
     * @return Vector
     */
    public static Vector ResultSetToVector(ResultSet rs)
        throws SQLException {
        Vector data = new Vector();
        while(rs.next()) {
            Vector row = new Vector();
            for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++){
                row.add(rs.getString(i));
            }
            data.add(row);
        }
        rs.close();
        return data;
    }

    /**
     * Clone two dim Vector
     * @param data Vector
     * @return Vector
     */
    public static Vector clone2DimVector(Vector data) {
        Vector newData = new Vector();
        for(int i = 0; i < data.size(); ++i) {
            Vector row = (Vector)data.elementAt(i);
            Vector newRow = new Vector();
            for(int j = 0; j < row.size(); ++j) {
                newRow.add(row.elementAt(j));
            }
            newData.add(newRow);
        }
        return newData;
    }

    /**
     * Clone one dim Vector
     * @param data Vector
     * @return Vector
     */
    public static Vector clone1DimVector(Vector data) {
        Vector newData = new Vector();
        for(int i = 0; i < data.size(); ++i) {
            newData.add(data.elementAt(i));
        }
        return newData;
    }

    /**
     * Clone two dim List
     * @param data List
     * @return List
     */
    public static List clone2DimList(List data) {
        List newData = new ArrayList();
        for(int i = 0; i < data.size(); ++i) {
            List row = (List)data.get(i);
            List newRow = new ArrayList();
            for(int j = 0; j < row.size(); ++j) {
                newRow.add(row.get(j));
            }
            newData.add(newRow);
        }
        return newData;
    }

    /**
     * Clone one dim List
     * @param data List
     * @return List
     */
    public static List clone1DimList(List data) {
        List newData = new ArrayList();
        for(int i = 0; i < data.size(); ++i) {
            newData.add(data.get(i));
        }
        return newData;
    }

    /**
     * 2 Dimension String Array To Vector
     * @param data String[][]
     * @return Vector
     */
    public static Vector String2DimToVector(String[][] data) {
        Vector newData = new Vector();
        for (int row = 0; row < data.length; row++) {
            Vector newRow = new Vector();
            for (int col = 0; col < data[row].length; col++) {
                newRow.add(data[row][col]);
            }
            newData.add(newRow);
        }
        return newData;
    }

    /**
     * 2 Dimension String Array To List
     * @param data String[][]
     * @return List
     */
    public static List String2DimToList(String[][] data) {
        List newData = new ArrayList();
        for (int row = 0; row < data.length; row++) {
            List newRow = new ArrayList();
            for (int col = 0; col < data[row].length; col++) {
                newRow.add(data[row][col]);
            }
            newData.add(newRow);
        }
        return newData;
    }
}
